#ifndef _CRIT_SECTION_H_
#define _CRIT_SECTION_H_

#include "Stdafx.h"
#include "Os_mutex.h"

#ifdef _LOCK_DEBUG
#define	MAX_DEBUG_LEN	20
#define OS_MAX_PATH		260

typedef struct tagLOCKINFO
{
	DWORD	dwFileLine;
	char	strFilePath[OS_MAX_PATH];
} LOCKINFO;
#endif

class ICritSection
{
public:
	virtual ~ICritSection(){};
#ifdef _LOCK_DEBUG
	virtual void	Lock(const char* strFilePath, DWORD dwFileLine)	= 0;
	virtual void	Unlock(const char* strFilePath, DWORD dwFileLine)	= 0;
#else
	virtual void	Lock() = 0;
	virtual void	Unlock() = 0;
#endif
};

class CCritSection :public ICritSection
{
	CCritSection (const CCritSection&);
	const CCritSection &operator = (const CCritSection&);
public:
	CCritSection( BYTE byLevel, const char* strOwnerName, void* pOwner );
	~CCritSection(void);

#ifdef _LOCK_DEBUG
	virtual void	Lock(const char* strFilePath, DWORD dwFileLine);
	virtual void	Unlock(const char* strFilePath, DWORD dwFileLine);
#else
	virtual void	Lock();
	virtual void	Unlock();
#endif
	
	inline  BYTE GetLevel();
	inline	char* GetOwnerName();
	inline  void* GetOwner();
	DWORD	GetLockSpan();

protected:
	void	EnterCritSection();
	void	LeaveCritSection();

protected:

	os_mutex_t	m_mutex;

	BYTE	m_level;
	char*	m_strOwnerName;
	void*	m_pOwner;

	// ²âÊÔ´úÂë
	DWORD	m_lockTime;

#ifdef _LOCK_DEBUG
	int			m_nLockCount;
	LOCKINFO	m_vecLockInfos[MAX_DEBUG_LEN];
#endif
};

inline BYTE CCritSection::GetLevel()
{
	return m_level;
}

inline char* CCritSection::GetOwnerName()
{
	return m_strOwnerName;
}

inline void* CCritSection::GetOwner()
{
	return m_pOwner;
}

#endif













